import { getUserInfo } from '../../utils/storage'
import { changeStatus, getHouseResLandLoadPage, delHouseRes } from '../../api/houseRes'
import config from '../../config/index'
//@ts-ignore
import Dialog from '@vant/weapp/dialog/dialog'
import { RoleEnum } from '../../enums/role'
Page({
  data: {
    list: [],
    userInfo: {} as any,
    config,
    resList: [] as any[],
    page: 1
  },
  getData() {
    const user = getUserInfo()
    if (user) {
      this.setData({
        userInfo: JSON.parse(user)
      })
    }
  },
  toAdd() {
    if (this.data.userInfo.role !== RoleEnum.VIPLANDLORD && this.data.resList.length === 1) {
      wx.showToast({
        title: '会员房东可上传多套房源',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '/pages/addHouse/addHouse'
    })
  },
  async initList() {
    const res = await getHouseResLandLoadPage(this.data.page, this.data.userInfo.id)
    if (res.statusCode === config.SUCCESS_CODE) {
      if (res.data.data.length === 0) return
      this.setData({
        resList: [
          ...this.data.resList,
          ...res.data.data
        ]
      })
    }
  },
  updateStatus(e: WechatMiniprogram.TouchEvent) {
    let id = e.currentTarget.dataset.id
    let status = e.currentTarget.dataset.status
    let title = status === true ? '停租' : '招租'
    let message = status === true ? '是否停租此房源？' : '是否招租此房源？'
    let data = {
      house_status: status
    }
    Dialog.confirm({
      title,
      message
    }).then(async () => {
      const res = await changeStatus(id, data)
      if (res.statusCode === config.SUCCESS_CODE) {
        wx.showToast({
          title: '更改成功',
          icon: 'none'
        })
        this.setData({
          page: 1,
          resList: []
        })
        this.initList()
      } else {
        wx.showToast({
          title: '更改失败请重试',
          icon: 'none'
        })
      }
    }).catch(() => { })
  },
  toDetail(e: WechatMiniprogram.TouchEvent) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/houseDetail/houseDetail?id=${id}`
    })
  },
  toUpdate(e: WechatMiniprogram.TouchEvent) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/updateRes/updateRes?id=${id}`
    })
  },
  async delHouse(e: WechatMiniprogram.TouchEvent) {
    Dialog.confirm({
      title: '删除房源',
      message: '是否删除此房源？'
    }).then(async () => {
      let id = e.currentTarget.dataset.id
      let house_plot = e.currentTarget.dataset.plot
      const res = await delHouseRes(id, house_plot)
      if (res.statusCode === config.CREATED_CODE || res.statusCode === config.SUCCESS_CODE) {
        wx.showToast({
          title: '删除成功',
          icon: 'none'
        })
        this.setData({
          page: 1,
          resList: []
        })
        this.initList()
      } else {
        wx.showToast({
          title: '删除失败',
          icon: 'none'
        })
      }
    }).catch(() => { })
  },
  onLoad() {
    this.getData()
  },
  onShow() {
    this.setData({
      page: 1,
      resList: []
    })
    this.initList()
  },
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    })
    this.initList()
  },
  onPullDownRefresh() {
    this.setData({
      page: 1,
      resList: []
    })
    this.initList()
  }
})